package com.kelvem.saas.workbanch.ontology.piaoorder.handle.core;

import com.kelvem.saas.workbanch.fsm.core.EventContext;
import com.kelvem.saas.workbanch.fsm.handle.Handle;
import com.kelvem.saas.workbanch.ontology.piaoorder.model.*;
import com.kelvem.saas.workbanch.ontology.piaoorder.request.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author kelvem
 */
@Service
@Slf4j
public class ConvRequest2PiaoOrderHandle extends Handle {
    
    @Override
    public void execute() {
    
        PiaoOrderReq req = (PiaoOrderReq) EventContext.getRequest();
        
        // order
        PiaoOrderEntity order = convert(req);
        EventContext.addContext(order);
    
        // goods
        PiaoOrderGoodsEntity goods = convert(req.getGoods());
        EventContext.addContext(goods);
        
        // collection
        PiaoOrderCollectionEntity collection = convert(req.getCollection());
        EventContext.addContext(collection);
    
        // address
        PiaoOrderAddressEntity sender = convert(req.getSender());
        sender.setAddressType("sender");
        EventContext.addContext(sender);
        PiaoOrderAddressEntity receiver = convert(req.getReceiver());
        receiver.setAddressType("receiver");
        EventContext.addContext(receiver);
    
        // total
        List<PiaoOrderTotalEntity> totalList = convertTotal(req.getPiaoOrderTotalReqList());
        EventContext.addContext(totalList);
        
        log.info("Handle[" + getName() + "] order=" + order);
        EventContext.print();
    }
    
    public PiaoOrderEntity convert(PiaoOrderReq req) {
        PiaoOrderEntity entity = new PiaoOrderEntity();
        BeanUtils.copyProperties(req, entity);
        return entity;
    }
    
    public PiaoOrderGoodsEntity convert(PiaoOrderGoodsReq req) {
        PiaoOrderGoodsEntity entity = new PiaoOrderGoodsEntity();
        BeanUtils.copyProperties(req, entity);
        return entity;
    }
    
    public PiaoOrderCollectionEntity convert(PiaoOrderCollectionReq req) {
        PiaoOrderCollectionEntity entity = new PiaoOrderCollectionEntity();
        BeanUtils.copyProperties(req, entity);
        return entity;
    }
    
    public PiaoOrderAddressEntity convert(PiaoOrderAddressReq req) {
        PiaoOrderAddressEntity entity = new PiaoOrderAddressEntity();
        BeanUtils.copyProperties(req, entity);
        return entity;
    }
    
    public List<PiaoOrderTotalEntity> convertTotal(List<PiaoOrderTotalReq> reqList) {
        
        List<PiaoOrderTotalEntity> result = new ArrayList<>();
        
        if (reqList == null || reqList.size() <= 0) {
            return result;
        }
        
        for (PiaoOrderTotalReq totalReq:
                reqList) {
            PiaoOrderTotalEntity entity = new PiaoOrderTotalEntity();
            BeanUtils.copyProperties(totalReq, entity);
            result.add(entity);
        }
        return result;
    }
}
